package com.hnote.hnoteapplication.view.widget;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hnote.hnoteapplication.R;
import com.hnote.hnoteapplication.utils.ScreenSizeUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/3/8 0008.
 */

public class HNoteDialog {

    private Context mContext;

    private HNoteClick mClick;

    public HNoteDialog(Context context, HNoteClick click){
        mContext = context;
        mClick = click;
    }

    /**
     * 自定义对话框
     */
    public void customDialog(String confirm, String cancle, String title, String message) {
        final Dialog dialog = new Dialog(mContext, R.style.NormalDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_normal, null);
        TextView cancelTv = (TextView) view.findViewById(R.id.cancel);
        TextView confirmTv = (TextView) view.findViewById(R.id.confirm);
        TextView titleTv = (TextView) view.findViewById(R.id.title);
        TextView messageTv = (TextView) view.findViewById(R.id.message);

        cancelTv.setText(cancle);
        confirmTv.setText(confirm);
        titleTv.setText(title);
        messageTv.setText(message);

        dialog.setContentView(view);
        //使得点击对话框外部不消失对话框
        dialog.setCanceledOnTouchOutside(true);
        //设置对话框的大小
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(mContext).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(mContext).getScreenWidth() * 0.75f);//设置对话框的宽度
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;//设置对话框的高度
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClick.cancelClick();
                dialog.dismiss();
            }
        });
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClick.confirmClick();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 自定义底部对话框
     */
    private void customBottomDialog() {
        /**
         *
         */
        Dialog dialog = new Dialog(mContext, R.style.NormalDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_bottom, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(mContext).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(mContext).getScreenWidth() * 0.9f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    /**
     * 圆形进度条对话框
     */
    private void circleProgressDialog() {
        ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setMessage("正在加载中");
        dialog.show();
    }

    /**
     * 直线进度条对话框
     */
    private void lineProgressDialog() {
        final ProgressDialog dialog = new ProgressDialog(mContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("正在加载中");
        dialog.setMax(100);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;

            @Override
            public void run() {
                dialog.setProgress(progress += 5);
                if (progress == 100) {
                    timer.cancel();
                }
            }
        }, 0, 1000);
        dialog.show();
    }


    public interface HNoteClick{
        void confirmClick();
        void cancelClick();
    }
}
